package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/request"
	"shop/common/global"
	"shop/shop_grpc/proto/shop"
)

func FindGoods(c *gin.Context) {
	var dev request.FindGoodsResult
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	goods, err := global.ShopClient.FindGoods(c, &shop.FindGoodsRequest{GoodsId: int64(dev.Id)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"data": goods,
	})
}

func AddShopCar(c *gin.Context) {
	var dev request.CreateShopCar
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	car, err := global.ShopClient.AddShopCar(c, &shop.AddShopCarRequest{
		UserId:  int64(dev.UserId),
		GoodsId: dev.GoodsId,
		Num:     dev.Num,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"data": car,
	})
}
